package org.blame.lotterysystem.controller;


import org.blame.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.blame.lotterysystem.common.exception.ControllerException;
import org.blame.lotterysystem.common.pojo.CommonResult;
import org.blame.lotterysystem.common.utils.JacksonUtil;
import org.blame.lotterysystem.controller.param.CreatePrizeParam;
import org.blame.lotterysystem.controller.param.PageParam;
import org.blame.lotterysystem.controller.result.FindPrizeListResult;
import org.blame.lotterysystem.service.PictureService;
import org.blame.lotterysystem.service.PrizeService;
import org.blame.lotterysystem.service.dto.PageListDTO;
import org.blame.lotterysystem.service.dto.PrizeDTO;
import org.glassfish.jaxb.runtime.v2.runtime.reflect.Lister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.stream.Collectors;

@RestController
public class PrizeController {

    private static final Logger logger= LoggerFactory.getLogger(PrizeController.class);

    @Autowired
    private PictureService pictureService;

    @Autowired
    private PrizeService prizeService;


    @RequestMapping("/pic/upload")
    public String uploadPicture(MultipartFile file){
        return pictureService.savePicture(file);
    }


    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic")MultipartFile file){

        logger.info("createPrize  CreatePrizeParam:{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizeService.insertPrize(param, file));
    }

    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param){
        logger.info("findPrizeList  param:{}",JacksonUtil.writeValueAsString(param));


        PageListDTO<PrizeDTO> pageListDTO=prizeService.findPrizeList(param);
        if (pageListDTO == null) {
            return CommonResult.success(new FindPrizeListResult(0, Collections.emptyList()));
        }
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));

    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (null == pageListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }

        FindPrizeListResult findPrizeListResult=new FindPrizeListResult();

        findPrizeListResult.setTotal(pageListDTO.getTotal());
        findPrizeListResult.setRecords(pageListDTO.getRecords().stream()
                .map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo=new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    return prizeInfo;
                }).collect(Collectors.toList()));
        return findPrizeListResult;
    }




}
